var form = layui.form,id=getQueryVariable("id"),pid=getQueryVariable("pid");
//当前部门
let department;
if(id==""){

}else{
    query( id);
    $("span")[0].innerHTML="编辑部门";
}

layer.ready(function () {
    //所有部门
    let deptList=[];
    $.ajax({
        url:'department/getAllDept',
        success:function (data) {
            deptList = data.data;
            for(let i in deptList){
                $('#pid').append(new Option(deptList[i].name, deptList[i].code));
            }
            for(let i in deptList){
                if (department.pid==deptList[i].pid){
                    $('#pid').val(deptList[i].pid);
                }
            }
            form.render('select');
        }
    })

});
//表单初始值，修改时调用
/*if(pid!=""){
    form.val("department-form",{
        "departmentname":"名称",
        "departmentid":"代码",
        "note":"备注"
    })
   // $("input[name='departmentid']").attr('readonly',true)
}*/

//监听提交
form.on('submit(department-form-element)', function(data){
    if(id==""){
        insert(data);
    }else{
        update(data);
    }

    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    layer.msg("保存成功", {icon: 6});
   setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
       parent.detab("views/jsp/SystemCenter/organization.jsp");
        parent.intab("views/jsp/SystemCenter/organization.jsp","组织结构");
        if(id==""){
            parent.detab("views/jsp/SystemCenter/tpl/newupdepartment.jsp?pid="+pid);
        }else{
            parent.detab("views/jsp/SystemCenter/tpl/newupdepartment.jsp?id="+id);
        }
       //parent.detab("views/jsp/SystemCenter/tpl/newupdepartment.jsp"+(pid==''?'pid='+pid:'?pid='+pid));
     //  parent.detab("views/jsp/SystemCenter/tpl/newupdepartment.jsp");
    },1000);

    return false;
});
function insert(data) {
    data.field.pid=pid;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'department/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function update(data) {
   data.field.id=id;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'department/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function query( id) {
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:{"id":id},
        url:'department/selectByKey',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                department=da.data;
                /* form.val("component-form",{
                     "companyname":da.data.name,
                     "companyid":da.data.code,
                     "note":da.data.desc
                 })*/
                $("#name").val(da.data.name);
                $("#code").val(da.data.code);
                $("#desc").val(da.data.desc);
                $("input[name='code']").attr('readonly',true)
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}